<template>
  <div>
    <el-card class="main-card"
             shadow="never"
             v-loading="$store.state.loading">
      <el-form ref="form"
               :model="form"
               label-width="130px"
               :rules="rules"
               :disabled="type === 3">
        <el-form-item label="指标名称"
                      prop="criteriaName">
          <el-input v-model="form.criteriaName"
                    placeholder="请输入指标名称"></el-input>
        </el-form-item>
        <el-form-item label="指标编号"
                      prop="criteriaCode">
          <el-input v-model="form.criteriaCode"
                    placeholder="请输入指标编号"></el-input>
        </el-form-item>
        <el-form-item label="上级指标编号">
          <el-input v-model="form.fkParentId"
                    placeholder="请输入上级指标编号"></el-input>
        </el-form-item>
        <el-form-item label="指标级别"
                      prop="criteriaLevel">
          <el-input v-model.number="form.criteriaLevel"
                    placeholder="请输入指标级别"></el-input>
        </el-form-item>
        <el-form-item label="指标分数"
                      prop="criteriaScore">
          <el-input v-model.number="form.criteriaScore"
                    placeholder="请输入指标分数"
                    type='number'></el-input>
        </el-form-item>
        <el-form-item label="排序"
                      prop="sortCode">
          <el-input v-model.number="form.sortCode"
                    placeholder="请输入排序"></el-input>
        </el-form-item>
        <el-form-item label="指标描述"
                      prop="description">
          <el-input v-model.number="form.description"
                    placeholder="请输入指标描述"
                    type="textarea"></el-input>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  props: {
    type: {
      type: Number,
      default: 1
    },
    madelId: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    const checkCriteriaScore = (rule, value, callback) => {
      if (value === null || !(typeof value === 'number')) {
        callback(new Error('请输入指标分数'))
      } else {
        callback()
      }
    }
    const checkCriteriaLevel = (rule, value, callback) => {
      if (value === null || !(typeof value === 'number')) {
        callback(new Error('请输入指标级别'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        criteriaName: [{ required: true, message: '请输入指标名称', trigger: 'change' }],
        criteriaCode: [{ required: true, message: '请输入指标编号', trigger: 'change' }],
        fkParentId: [{ required: true, message: '请输入上级指标编号', trigger: 'change' }],
        criteriaLevel: [{ required: true, validator: checkCriteriaLevel, trigger: 'blur' }],
        criteriaScore: [{ required: true, validator: checkCriteriaScore, trigger: 'blur' }],
        // recurringIncome: [{ required: true, validator: checkRecurringIncome, trigger: 'blur' }],
      },
      form: {
        criteriaCode: "",
        criteriaLevel: 0,
        criteriaName: "",
        criteriaScore: 0,
        description: "",
        fkParentId: "",
        id: "",
        memo: "",
        sortCode: 0,
        version: 0
      },
      deptList: [],
      action: {
        def: '/ruralAssessmentCriteria/'
        // getDept: '/accountDept/listSelectDeptNames'
      },
    }
  },
  created () {
    // this.getDeptInfo()
    if (this.type !== 1) {
      this.getPageList()
    }
  },
  methods: {
    // getDeptInfo () {
    //   this.$Ajax(
    //     this.action.getDept,
    //     'get',
    //     {},
    //     (res) => {
    //       this.deptList = res.data.filter(item => item.fkParentCode)
    //     }
    //   )
    // },
    col () {
      this.$emit('colMalde')
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (!valid) return
        this.$Ajax(
          this.action.def,
          this.type === 1 ? 'post' : 'put',
          this.form,
          (res) => {
            this.$message.success(res.message);
            this.$emit('getdata')
            this.col()
          }
        )
      })
    },
    getPageList () {
      this.$Ajax(
        this.action.def + this.madelId,
        "get",
        {
        },
        (res) => {
          console.log(res.data)
          Object.keys(this.form).forEach(key => {
            if (res.data[key] !== undefined) {
              this.form[key] = res.data[key]
              if (key === 'statisticsYear') {
                this.form[key] = res.data[key].toString()
              }
            }
          })
        }
      );
    },
  }
}
</script>

<style lang="scss" scoped>
/deep/.el-input__inner {
  padding-right: 0px;
}
.footer-btn {
  text-align: right;
}
.divider {
  margin-bottom: 10px;
}
</style>